package cc.grapevine.netcard.apiservlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import cc.grapevine.netcard.ado.BasicinfoADO;
import cc.grapevine.netcard.bean.BasicinfoBean;
import cc.grapevine.netcard.tools.ApiString;

public class BasicinfoServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	BasicinfoADO basicinfoDao = new BasicinfoADO();
	Gson gson = new Gson();

	/**
	 * Constructor of the object.
	 */
	public BasicinfoServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		// do my things .
		String mail = (String) request.getParameter(ApiString.MAIL);
		String app = (String) request.getParameter(ApiString.APP);
		
		int errorFlag = 0;
		BasicinfoBean basicInfo = null;
		System.out.println("===" + mail + "=============" + app + "=");
		if (!ApiString.APP_VALUE.equals(app)) {
			errorFlag = -1;
		} else if ("".equals(mail) || mail == null) {
			errorFlag = -1;
		} else {

			 basicInfo = basicinfoDao.getBasicinfo(mail);

			if (null != basicInfo) {

				errorFlag = 1;
			} else {
				errorFlag = -1;

			}
			
		}  
		
		if (errorFlag == 1) {
			basicInfo.setError(1);
			out.println(gson.toJson(basicInfo));
		} else {
			BasicinfoBean  errorBean = new BasicinfoBean();
			errorBean.setError(-1);
			out.println(gson.toJson(errorBean));
		}
		out.flush();
		out.close();
	}



	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
